﻿using Sunny.UI;
using System;
using WorkSplit;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MES数据展示
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Location = new Point(0, 0);
            this.WindowState
                = FormWindowState.Maximized;
            pictureBox1.BackgroundImage = Properties.Resources.thlogo2;
            InitMain();
        }
        public void InitMain()
        {
            int pageIndex = 100;
            TreeNode parent = uiMenu.CreateNode("A线 TH42173205Fe", pageIndex);
            uiMenu.CreateChildNode(parent, "前段", ++pageIndex);
            uiMenu.CreateChildNode(parent, "中段", ++pageIndex);
            uiMenu.CreateChildNode(parent, "后段", ++pageIndex);
            pageIndex = 200;
            parent = uiMenu.CreateNode("B线 71173204Fe", pageIndex);
            uiMenu.CreateChildNode(parent, "前段", ++pageIndex);
            uiMenu.CreateChildNode(parent, "中段", ++pageIndex);
            uiMenu.CreateChildNode(parent, "后段", ++pageIndex);
        }

        List<A线前中后分离数据> resA;
        List<B线前中后分离数据> resB;
        private async void Form1_Load(object sender, EventArgs e)
        {
            while (true)
            {
                Start 分离数据获取 = new Start();
                await 分离数据获取.获取所有工段数据();

                //此代码要单独封装事件
                resA = 分离数据获取.A线分离处理();
                resB = 分离数据获取.B线分离处理();

                //首先运行尝试查看效果，不可省略
                Menu_MenuItemClick(null, null, 101);

                for (int i = 0; i < 10; i++)
                {
                    await Task.Delay(1000);
                }
            }
        }

        public bool Check()
        {
            if (resA == null || resB == null)
            {
                return false;
            }
            return true;

        }

        public void A线前段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option A线前段 = new UIBarOption返回Option();
            UIOption A = A线前段.Start(resA[0].Qian, "投入数量", "良品数量", "不良数量");
            uiBarChart1.SetOption(A);
        }
        public void A线中段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option A线中段 = new UIBarOption返回Option();
            UIOption A = A线中段.Start(resA[0].Zhong, "投入数量", "良品数量", "不良数量");
            uiBarChart2.SetOption(A);
        }
        public void A线后段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option A线后段 = new UIBarOption返回Option();
            UIOption A = A线后段.Start(resA[0].Hou, "投入数量", "良品数量", "不良数量");
            uiBarChart3.SetOption(A);
        }
        public void B线前段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option B线后段 = new UIBarOption返回Option();
            UIOption B = B线后段.Start(resB[1].Qian, "投入数量", "良品数量", "不良数量");
            uiBarChart4.SetOption(B);
        }
        public void B线中段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option B线后段 = new UIBarOption返回Option();
            UIOption B = B线后段.Start(resB[1].Zhong, "投入数量", "良品数量", "不良数量");
            uiBarChart5.SetOption(B);
        }
        public void B线后段()
        {
            if (Check() == false)
            {
                return;
            }
            UIBarOption返回Option B线后段 = new UIBarOption返回Option();
            UIOption B = B线后段.Start(resB[1].Hou, "投入数量", "良品数量", "不良数量");
            uiBarChart6.SetOption(B);
        }

        private void Menu_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {
            switch (pageIndex)
            {
                //case 101:
                //    A线前段();
                //    uiBarChart1.BringToFront();
                //    break;
                case 102:
                    A线中段();
                    uiBarChart2.BringToFront();
                    break;
                case 103:
                    A线后段();
                    uiBarChart3.BringToFront();
                    break;
                //case 201:
                //    B线前段();
                //    uiBarChart4.BringToFront();
                //    break;
                case 202:
                    B线中段();
                    uiBarChart5.BringToFront();
                    break;
                case 203:
                    B线后段();
                    uiBarChart6.BringToFront();
                    break;
                default:
                    break;
            }
        }

        int taskNum = 0;
        const int nnn = 5000;
        private async void Form1_Resize(object sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.Maximized)
            {
                taskNum = 0;
                uiMenu.Visible = false;

                while (true)
                {
                    if (taskNum == 1)
                    {
                        break;
                    }
                    Menu_MenuItemClick(null, null, 101);
                    await Task.Delay(nnn);
                    Menu_MenuItemClick(null, null, 102);
                    await Task.Delay(nnn);
                    Menu_MenuItemClick(null, null, 103);
                    await Task.Delay(nnn);
                    if (taskNum == 1)
                    {
                        break;
                    }
                    Menu_MenuItemClick(null, null, 201);
                    await Task.Delay(nnn);
                    Menu_MenuItemClick(null, null, 202);
                    await Task.Delay(nnn);
                    Menu_MenuItemClick(null, null, 203);
                    await Task.Delay(nnn);
                    if (taskNum == 1)
                    {
                        break;
                    }
                }
            }
            else if (this.WindowState == FormWindowState.Normal)
            {
                uiMenu.Visible = true;
                taskNum = 1;
            }
        }

    }
}
